﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Windows.Forms;

namespace DataDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //{"dataId":1,"imageFile":"","voiceFile":""}

            //{"code":"00000","errorMsg":"Error","mediaInfo":{"dataId":1,"imageFile":"","voiceFile":""}}

            System.Windows.Forms.OpenFileDialog _OpenFile = new OpenFileDialog();
            _OpenFile.Multiselect = false;
            _OpenFile.DefaultExt = "图片文件 (*.png)|*.png (*.jpg)|.jpg";
            //_OpenFile.InitialDirectory = "C://Destop";


            if (_OpenFile.ShowDialog() == DialogResult.OK)
            {
                Console.WriteLine(_OpenFile.FileName);
            }



            string _Json = "{\"code\":\"00000\",\"errorMsg\":\"Error\",\"mediaInfo\":{\"dataId\":1,\"imageFile\":\"Image路径\",\"voiceFile\":\"Voice路径\"}}";
            string toDes = "{\"dataId\":1,\"imageFile\":\"Image路径\",\"voiceFile\":\"Voice路径\"}";
            //string to = "{\"ID\":\"1\",\"Name\":\"曹操\",\"Sex\":\"男\",\"Age\":\"1230\"}";
            using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(_Json)))
            {
                DataContractJsonSerializer deseralizer = new DataContractJsonSerializer(typeof(JSONSDemo));
                JSONSDemo model = (JSONSDemo)deseralizer.ReadObject(ms);// //反序列化ReadObject
                //Console.WriteLine("dataId=" + model.dataId);
                //Console.WriteLine("imageFile=" + model.imageFile);
                //Console.WriteLine("voiceFile=" + model.voiceFile);
            }
            
            //DBUtility.DBControl dBControl = new DBUtility.DBControl();

            //List<DBUtility.tb_User> list = dBControl.SelectAll<DBUtility.tb_User>();
            //foreach (var item in list)
            //{
            //    Console.WriteLine(item.userName.ToString() + " " + item.passWord.ToString() + "   ");
            //}

            Console.WriteLine(Guid.NewGuid().ToString().Replace("-", ""));


            using (FileStream _FileStream = File.OpenRead("E:\\20181119091449.png"))
            {
                byte[] _FileByte = new byte[_FileStream.Length];
                _FileStream.Read(_FileByte, 0, _FileByte.Length);
                _FileStream.Close();
                _FileStream.Dispose();
                Console.WriteLine(Convert.ToBase64String(_FileByte));
            }


            byte[] bytes = { 1, 2, 3, 4, 5 };

            Console.WriteLine(BitConverter.ToString(bytes));

            string base64Str = Convert.ToBase64String(bytes);
            Console.WriteLine(base64Str);
            byte[] _ImageData = Convert.FromBase64String(base64Str);

            Console.WriteLine(DateTime.Now.ToString());

            byte[] byteArray = System.Text.Encoding.Default.GetBytes(BitConverter.ToString(bytes));

            string s1 = "测试";
            char c1 = 'A';
            char c2 = 'a';
            char c3 = '测';
            Console.WriteLine(BitConverter.GetBytes(c1).Length);
            Console.WriteLine(BitConverter.GetBytes(c2).Length);
            Console.WriteLine(BitConverter.GetBytes(c3).Length);

            string ConntionString = GetSqlConntion();
            SqlConnection sqlConnection = new SqlConnection(ConntionString);
            DataSet ds = new DataSet();
            DataTable dt;
            string Script = "Select Count(CreateDate) From tb_AlarminstrumentHisData Group by CreateDate Order By CreateDate";

            //SqlDataAdapter adapter = new SqlDataAdapter(Script, sqlConnection);
            //adapter.Fill(ds);
            //dt = ds.Tables[0];
            //adapter.Dispose();

            using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
            {
                sqlConnection.Open();

                sqlCommand.CommandText = Script;
                Object Result = sqlCommand.ExecuteScalar();

            }

            sqlConnection.Close();
            sqlConnection.Dispose();


            //using (var dbContext = new DBUtility.LMPAlarmEntities())
            //{
            //    dbContext.Configuration.LazyLoadingEnabled = true; // 默认是true，针对导航属性
            //    var categoryList = dbContext.Set<DBUtility.tb_User>().ToList();
            //    // 只会在数据库里面查询Category表，不会查询ProductDetail表
            //    foreach (var category in categoryList)
            //    {
            //        Console.WriteLine("CategoryId:" + category.userName + ",CategoryName:" + category.passWord + " " + category.tb_RoleInfo.Name);
            //        // 这时才会去数据库查询ProductDetail表
            //    }
            //}

            Console.ReadKey();
        }

        public static string GetSqlConntion()
        {
            SqlConnectionStringBuilder sqlConnectionStringBuilder = new SqlConnectionStringBuilder()
            {
                DataSource = "192.168.1.233",
                UserID = "sa",
                Password = "123456",
                InitialCatalog = "LMPAlarm",
                IntegratedSecurity = false
            };

            return sqlConnectionStringBuilder.ToString();
        }
    }
}
